package cn.iocoder.yudao.module.erp.controller.admin.product;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductPurchaseInfoRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ProductPurchaseInfoSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductPurchaseInfoDO;
import cn.iocoder.yudao.module.erp.service.product.ErpProductPurchaseInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 产品- 采购资料")
@RestController
@RequestMapping("/erp/product/purchaseInfo")
@Validated
public class ErpProductPurchaseInfoController {

    @Resource
    private ErpProductPurchaseInfoService productPurchaseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建产品采购资料")
    @PreAuthorize("@ss.hasPermission('erp:product:create')")
    public CommonResult<Long> createProduct(@Valid @RequestBody ProductPurchaseInfoSaveReqVO createReqVO) {
        return success(productPurchaseInfoService.createProductPurchaseInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品采购资料")
    @PreAuthorize("@ss.hasPermission('erp:product:update')")
    public CommonResult<Boolean> updateProduct(@Valid @RequestBody ProductPurchaseInfoSaveReqVO updateReqVO) {
        productPurchaseInfoService.updateProductPurchaseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品采购资料")
    @Parameter(name = "fid", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:product:delete')")
    public CommonResult<Boolean> deleteProductPurchaseInfo(@RequestParam("id") Long fid) {
        productPurchaseInfoService.deleteProductPurchaseInfo(fid);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品采购资料")
    @Parameter(name = "parentId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<ErpProductPurchaseInfoRespVO> getProduct(@RequestParam("parentId") Long parentId) {
        ErpProductPurchaseInfoDO productPurchaseInfo = productPurchaseInfoService.getProductPurchaseInfo(parentId);
        if(productPurchaseInfo == null){
            productPurchaseInfo = new ErpProductPurchaseInfoDO();
            productPurchaseInfo.setFmaterialid(parentId);
        }
        return success(BeanUtils.toBean(productPurchaseInfo, ErpProductPurchaseInfoRespVO.class));
    }
}